angular.module('mamApp').controller('firstLevelChannelController', ['$scope', '$state', '$uibModal','ImsDialog','ChannelManager','$filter',
    function ($scope, $state, $uibModal, ImsDialog, ChannelManager, $filter) {

    $scope.search = {
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10
    };

    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)
        }
    };
    $scope.noPermission=false;
    ChannelManager.categoriesList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        if(result.data.c=="n"){
            $scope.noPermission=true;
        }else{
            $scope.noPermission=false;
            $scope.categoriesList = result.data.pageList;
            $scope.paginationConf.totalItems = result.data.total;
        }


    });

    //初始化全选值为false
    $scope.checkData={allChecked:false};
    //全选
    $scope.selectAllChange=function (checked) {
        $scope.categoriesList.forEach(function (item) {
            item.checked=checked;
        })
    };
    //选择一个
    $scope.selectOneChange=function (checked) {
        if(!checked){
            $scope.checkData.allChecked=false;
        }else {
            ($filter('filter')($scope.categoriesList,{checked:true}).length)==$scope.categoriesList.length?$scope.checkData.allChecked=true:$scope.checkData.allChecked=false;
        }
    };

    //新建数据
    $scope.create = function () {
        $scope.formData = {};
        $scope.formData.status = '2';
        var modalInstance = $uibModal.open({
            animation: true,
            size: 'normal',
            openedClass:"modal-open-normal",
            templateUrl: 'oneLevelModel.html',
            controller: 'oneLevelModelController',
            resolve: {
                data:function () {
                    return $scope.formData;
                }
            }
        });
        modalInstance.result.then(function (result) {
            if (result=="ok"){
                $state.reload();
            }
/*            if (result=="error"){
                ImsDialog.open({title:"频道管理",message:"频道名称已存在。"}).then(function (a) {})
            }*/
        })
    };

    //编辑数据
    $scope.edit = function (item) {
        $scope.formData = {};
        $scope.formData.categoryId = item.categoryId;
        $scope.formData.categoryName = item.categoryName;
        $scope.formData.status = item.status;
        var modalInstance = $uibModal.open({
            animation: true,
            size: 'normal',
            openedClass:"modal-open-normal",
            templateUrl: 'oneLevelModel.html',
            controller: 'oneLevelModelController',
            resolve: {
                data:function () {
                    return $scope.formData;
                }
            }
        });
        modalInstance.result.then(function (result) {
            if (result=="ok"){
                $state.reload();
            }
            /*if (result=="error"){
                ImsDialog.open({title:"频道管理",message:"频道名称已存在。"}).then(function (a) {})
            }*/
        })
    };

    var statusChange = {};

    //禁用一个
    $scope.disable=function (item) {
        statusChange.status = '2';
        ImsDialog.open({title:"频道管理",message:'确定禁用该频道？<br><small style="color: #CCCCCC">注：禁用频道不影响专辑，但新建专辑不能再选择本频道</small>'}).then(function (a) {
            if(a=="ok"){
                ChannelManager.categoryStatusChange(item.categoryId,statusChange).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload();
                    }
                })
            }
        })
    };

    //启用一个
    $scope.enable=function (item) {
        statusChange.status = '4';
        ImsDialog.open({title:"频道管理",message:'确定启用该频道？'}).then(function (a) {
            if(a=="ok"){
                ChannelManager.categoryStatusChange(item.categoryId,statusChange).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload();
                    }
                })
            }
        })
    };

    //禁用多个
    $scope.disableAll=function () {
        var tempArray = [];
        $scope.categoriesList.forEach(function (item) {
            if(item.checked == true){
                if(item.status == '4'){
                    tempArray.push(item.categoryId)
                }
            }
        });
        statusChange.status = '2';
        if(tempArray.length>0){
            ImsDialog.open({title:"频道管理",message:'确定禁用该频道？<br><small style="color: #CCCCCC">注：禁用频道不影响专辑，但新建专辑不能再选择本频道</small>'}).then(function (a) {
                if(a=="ok"){
                    ChannelManager.categoryStatusChange(tempArray,statusChange).then(function (result) {
                        if(result.data.c=="s"){
                            $state.reload();
                        }
                    })
                }
            })
        } else {
            return false;
            //ImsDialog.open({title:"频道管理",message:"没有可禁用的数据。"}).then(function (a) {})
        }
    };

    //启用多个
    $scope.enableAll=function () {
        var tempArray = [];
        $scope.categoriesList.forEach(function (item) {
            if(item.checked == true){
                if(item.status == '2'){
                    tempArray.push(item.categoryId)
                }
            }
        });
        statusChange.status = '4';
        if(tempArray.length>0){
            ImsDialog.open({title:"频道管理",message:'确定启用该频道？'}).then(function (a) {
                if(a=="ok"){
                    ChannelManager.categoryStatusChange(tempArray,statusChange).then(function (result) {
                        if(result.data.c=="s"){
                            $state.reload();
                        }
                    })
                }
            })
        } else {
            return false;
            //ImsDialog.open({title:"频道管理",message:"没有可启用的数据。"}).then(function (a) {})
        }
    };

    //删除数据
    $scope.delete=function () {
        var tempArray = [];
        $scope.categoriesList.forEach(function (item) {
            if(item.checked == true){
                tempArray.push(item.categoryId)
            }
        });
        if(tempArray.length>0){
            ImsDialog.open({title:"频道管理",message:'确定删除该频道？<br><small style="color: #CCCCCC">注：删除频道不影响专辑，但新建专辑不能再选择本频道</small>'}).then(function (a) {
                if(a=="ok"){
                    ChannelManager.categoryDelete(tempArray).then(function (result) {
                        if(result.data.c=="s"){
                            $state.reload();
                        }
                    })
                }
            })
        } else {
            return false;
            //ImsDialog.open({title:"频道管理",message:"没有可删除的数据。"}).then(function (a) {})
        }
    }


}]);

